var express = require('express');
var router = express.Router();
var { groupLabelModel, groupModel } = require('../../../model/model');

/* GET home page. */

// 团长审核-0--------------------------------------------------------------------
router.get('/getgroups', async (req, res, next) => {
  let group_list = await groupModel.find();
  let group_label = await groupLabelModel.find();
  res.send({
    code: 200,
    group_list,
    group_label
  });
});


//通过
router.post('/updategroup_ok', async (req, res, next) => {
  let { id, user } = req.body.data
  await groupModel.updateOne({ _id: id }, { examine_type: '已通过', examine_name: user })
  res.send({
    code: 200
  })
})

//作废
router.post('/updategroup_no', async (req, res, next) => {
  let { id, text, user } = req.body.data
  await groupModel.updateOne({ _id: id }, { examine_type: '已作废', void_text: text, examine_name: user })
  res.send({
    code: 200
  })
})
//撤回
router.post('/updategroup_out', async (req, res, next) => {
  let { id } = req.body.data
  await groupModel.updateOne({ _id: id }, { examine_type: '审核中' })
  res.send({
    code: 200
  })
})

//批量通过
router.post('/updategroup_all_ok', async (req, res, next) => {
  let { ids, user } = req.body.data
  await groupModel.updateMany({ _id: { $in: ids } }, { examine_type: '已通过', examine_name: user })
  res.send({
    code: 200
  })
})

//批量作废
router.post('/updategroup_all_no', async (req, res, next) => {
  let { ids, user } = req.body.data
  await groupModel.updateMany({ _id: { $in: ids } }, { examine_type: '已作废', examine_name: user })
  res.send({
    code: 200
  })
})

//组合搜索
router.post('/group_search', async (req, res, next) => {
  let { name, tel, pickup, address, type } = req.body.data.look_list
  let search_list = []
  if (name) search_list.push({ name: RegExp(name) })
  if (tel) search_list.push({ tel: RegExp(tel) })
  if (pickup) search_list.push({ pickup: RegExp(pickup) })
  if (address) search_list.push({ address: RegExp(address) })
  if (type) search_list.push({ type: type })
  let result = await groupModel.find({ $and: search_list })
  res.send({
    code: 200,
    data: result
  })
})

// 团长管理----------------------------------------------------------
//修改无效
router.post('/updateGroup_statue', async (req, res, next) => {
  let { id } = req.body.data
  await groupModel.updateOne({ _id: id }, { statue: '无效' })
  res.send({
    code: 200,
  })
})

//修改有效
router.post('/updateGroup_statue_ok', async (req, res, next) => {
  let { id } = req.body.data
  await groupModel.updateOne({ _id: id }, { statue: '有效' })
  res.send({
    code: 200,
  })
})

//批量查询
router.post('/look_list_Administration', async (req, res, next) => {
  let { name, tel, pickup, address, type, label } = req.body.data.look_list_Administration
  let search_list = []
  if (name) search_list.push({ name: RegExp(name) })
  if (tel) search_list.push({ tel: RegExp(tel) })
  if (pickup) search_list.push({ pickup: RegExp(pickup) })
  if (address) search_list.push({ address: RegExp(address) })
  if (type) search_list.push({ type: type })
  if (label) search_list.push({ label: { $in: label } })
  let result = await groupModel.find({ $and: search_list })
  res.send({
    code: 200,
    data: result
  })
})
//添加标签
router.post('/addGroupTag', async (req, res, next) => {
  let { administrationCheck, is_lable } = req.body.data
  let list = await groupModel.find({ _id: { $in: administrationCheck } }).lean()
  for (let i in list) {
    for (let item in is_lable) {
      if (list[i].label.indexOf(is_lable[item]) == -1) {
        list[i].label.push(is_lable[item])
      }
    }
  }
  for (let i in list) {
    await groupModel.updateOne({ _id: list[i]._id }, { label: list[i].label })
  }
  res.send({
    code: 200,
    data: '修改成功'
  })
})
//批量添加有效
router.post('/updateLable_all_ok', async (req, res, next) => {
  let { administrationCheck } = req.body.data
  await groupModel.updateMany({ _id: { $in: administrationCheck } }, { statue: '有效' })
  res.send({
    code: 200,
    data: '修改成功'
  })
})
//批量添加无效
router.post('/updateLable_all_no', async (req, res, next) => {
  let { administrationCheck } = req.body.data
  await groupModel.updateMany({ _id: { $in: administrationCheck } }, { statue: '无效' })
  res.send({
    code: 200,
    data: '修改成功'
  })
})

//标签搜素
router.post('/tag_search', async (req, res, next) => {
  let { label_look } = req.body.data
  console.log(req.body.data);
  let result = await groupLabelModel.find({ name: RegExp(label_look) })
  res.send({
    code: 200,
    data: result
  })
})

//添加标签
router.post('/addTag', async (req, res, next) => {
  let { name, color,describe,statue } = req.body.data
  await groupLabelModel.create({ name, color,describe,statue })
  res.send({
    code: 200,
    data: '添加成功'
  })
})

//标签修改
router.post('/upTag', async (req, res, next) => {
  let { id,name, color,describe,statue } = req.body.data
  await groupLabelModel.updateOne({ _id: id }, { name, color,describe,statue })
  res.send({
    code: 200,
    data: '修改成功'
  })
})

module.exports = router;
